[global set_idt]
[extern idtd]
[extern isr_handler]

%macro ISR_NOERRCODE 1  ; define a macro, taking one parameter
  [GLOBAL isr%1]        ; %1 accesses the first parameter.
  isr%1:
    cli
    push byte 0
    push byte %1
    jmp isr_stub_handler
%endmacro

%macro ISR_ERRCODE 1
  [GLOBAL isr%1]
  isr%1:
    cli
    push byte %1
    jmp isr_stub_handler
%endmacro 

set_idt:
	lidt [idtd]
	ret

ISR_NOERRCODE 1
ISR_NOERRCODE 2
ISR_NOERRCODE 3
ISR_NOERRCODE 4
ISR_NOERRCODE 5
ISR_NOERRCODE 6
ISR_NOERRCODE 7
ISR_ERRCODE 8
ISR_NOERRCODE 9
ISR_ERRCODE 0xA
ISR_ERRCODE 0xB
ISR_ERRCODE 0xC
ISR_ERRCODE 0xD
ISR_ERRCODE 0xE
ISR_NOERRCODE 0xF
ISR_NOERRCODE 0x10
ISR_NOERRCODE 0x11
ISR_NOERRCODE 0x12
ISR_NOERRCODE 0x13
ISR_NOERRCODE 0x14
ISR_NOERRCODE 0x15
ISR_NOERRCODE 0x16
ISR_NOERRCODE 0x17
ISR_NOERRCODE 0x18
ISR_NOERRCODE 0x19
ISR_NOERRCODE 0x1A
ISR_NOERRCODE 0x1B
ISR_NOERRCODE 0x1C
ISR_NOERRCODE 0x1D
ISR_NOERRCODE 0x1E
ISR_NOERRCODE 0x1F



isr_stub_handler:
	pusha
	push ds
	push es
	push fs
	push gs
	mov ax, 0x10  ; Load the Kernel Data Segment descriptor!
	mov ds, ax
	mov es, ax
	mov fs, ax
	mov gs, ax
	mov eax, esp
	push eax
	mov eax, isr_handler
	call [eax]       ; A special call, preserves the 'eip' register
	pop eax
	pop gs
	pop fs
	pop es
	pop ds
	popa
	add esp, 0x8
	iret

